﻿using PHMEE.Constants;
using PHMEE.ToolKits;
using System;
using System.Collections.Generic;
using System.Globalization;
using Volo.Abp;
using Volo.Abp.Domain.Entities.Auditing;

namespace PHMEE
{
    /// <summary>
    /// 部件
    /// </summary>
    public abstract class Component : CreationAuditedEntity<Guid>
    {
        protected Component()
        {

        }

        public Component(Guid id, Guid equipmentId, string name, string nameen, int number) : base(id)
        {
            EquipmentId = equipmentId;
            SetName(name);
            SetNameEn(nameen);
            SetNumber(number);
        }


        /// <summary>
        /// 名称
        /// </summary>
        public string Name { get; private set; }

        /// <summary>
        /// 英文名
        /// </summary>
        public string NameEn { get; private set; }

        /// <summary>
        /// 编号
        /// </summary>
        public int Number { get; private set; }

        /// <summary>
        /// 标签
        /// </summary>
        public string Tags { get; set; }

        /// <summary>
        /// 型号
        /// </summary>
        public string Model { get; set; }

        /// <summary>
        /// 类型
        /// </summary>
        public ComponentEnum Type { get; set; }

        public Guid EquipmentId { get; set; }

        public string Language => CultureInfo.CurrentCulture.TwoLetterISOLanguageName;

        /// <summary>
        /// 多语言展示Name
        /// </summary>
        public string DisplayName => GetLocalizedName();

        public Dictionary<string, object> DerivedPropertiesJson { get; set; } = new Dictionary<string, object>();

        public abstract void SerializeProperties(Dictionary<string, object> derivedPropertiesJson);
        public abstract void DeserializeProperties();


        public Component SetName(string name)
        {
            Name = Check.NotNullOrEmpty(name, nameof(name), ComponentConst.MaxNameLength);
            return this;
        }

        public Component SetNameEn(string nameen)
        {
            NameEn = Check.NotNullOrEmpty(nameen, nameof(nameen), ComponentConst.MaxNameLength);
            return this;
        }

        public Component SetNumber(int number)
        {
            Number = number;
            return this;
        }

        public List<BaseInfoTagEnum> GetComponentTagOptions()
        {
            var list = new List<BaseInfoTagEnum>();

            if (!string.IsNullOrEmpty(Tags))
            {
                var arr = Tags.Split(',', StringSplitOptions.RemoveEmptyEntries);

                foreach (var tag in arr)
                {
                    if (Enum.TryParse(tag, out BaseInfoTagEnum result))
                    {
                        list.Add(result);
                    }
                }
            }

            return list;
        }

        public static List<BaseInfoTagEnum> GetLevelTagOptions()
        {
            var list = PHMEECoreHelper.PhmeeGetEnumsFilter<BaseInfoTagEnum>((int)BaseInfoTagEnum.Power);

            return list;

        }

        private string GetLocalizedName()
        {
            string localizedName = Language switch
            {
                PhmeeConst.LanguageEn => NameEn,
                _ => Name
            };

            return localizedName;
        }
    }
}
